﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CP.Utility;

namespace GPM
{
    public partial class BaseEditForm : BaseForm
    {
        public string EntityID { get; set; }
        public BaseEditForm()
        {
            InitializeComponent();
        }

        //protected override void PrivilegeControl()
        //{
        //    return;
        //}

        public void FormOnLoad()
        {
            //base.FormOnLoad();
            if (!this.DesignMode)
            {
                if (!string.IsNullOrEmpty(EntityID))
                {
                    this.Text = "编辑 " + this.Text;
                    this.btnAdd.Visible = false;//如果是编辑，则屏蔽添加按钮
                }
                else
                {
                    this.Text = "新建 " + this.Text;
                    this.btnOK.Visible = false;
                    this.btnAdd.Left = 105;
                }

                DisplayData();
            }
        }

        /// <summary>
        /// 显示数据到控件上
        /// </summary>
        public virtual void DisplayData()
        {
        }

        /// <summary>
        /// 检查输入的有效性
        /// </summary>
        /// <returns>有效</returns>
        public virtual bool CheckInput()
        {
            return true;
        }

        /// <summary>
        /// 清除屏幕
        /// </summary>
        public virtual void ClearScreen()
        {
            this.EntityID = "";////需要设置为空，表示新增
            ClearControlValue(this);
            this.FormOnLoad();
        }

        public virtual void ClearControlValue(Control container)
        {
            ControlExtension.GetControls<CheckEdit>(this).ForEach(c => c.Checked = false);
            ControlExtension.GetControls<ComboBoxEdit>(this).ForEach(c => c.Text = "");
            ControlExtension.GetControls<DateEdit>(this).ForEach(c => c.Text = "");
            ControlExtension.GetControls<LookUpEdit>(this).ForEach(c => c.EditValue = null);
            ControlExtension.GetControls<TextEdit>(this).ForEach(c => c.Text = "");
        }

        /// <summary>
        /// 保存数据（新增和编辑的保存）
        /// </summary>
        public virtual bool SaveEntity()
        {
            bool result = false;
            if (!string.IsNullOrEmpty(EntityID))
            {
                //编辑的保存
                result = SaveUpdated();
            }
            else
            {
                //新增的保存
                result = SaveAddNew();
            }

            return result;
        }

        /// <summary>
        /// 更新已有的数据
        /// </summary>
        /// <returns></returns>
        public virtual bool SaveUpdated()
        {
            return true;
        }

        /// <summary>
        /// 保存新增的数据
        /// </summary>
        /// <returns></returns>
        public virtual bool SaveAddNew()
        {
            return true;
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="close">关闭窗体</param>
        private void SaveEntity(bool close)
        {
            // 检查输入的有效性
            if (this.CheckInput())
            {
                // 设置鼠标繁忙状态
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    if (this.SaveEntity())
                    {
                        ExMessageBox.Show("保存成功！", AppSetter.SoftName);
                        if (close)
                        {
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        //else
                        //{
                        //    this.ClearScreen();
                        //}
                    }
                }
                catch (Exception ex)
                {
                    ExMessageBox.Show(ex.Message, AppSetter.SoftName);
                }
                finally
                {
                    // 设置鼠标默认状态
                    this.Cursor = Cursors.Default;
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.SaveEntity(false);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.SaveEntity(true);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F5)
            {
                this.FormOnLoad();
            }

            if (!(ActiveControl is SimpleButton))
            {
                if (keyData == Keys.Down || keyData == Keys.Enter)
                {
                    return this.SelectNextControl(this.ActiveControl, true, true, true, true);
                }
                else if (keyData == Keys.Up)
                {
                    return this.SelectNextControl(this.ActiveControl, false, true, true, true);
                }

                return false;
            }
            else
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }

        private void BaseEditForm_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                // 设置鼠标繁忙状态
                this.Cursor = Cursors.WaitCursor;
                try
                { 
                    this.FormOnLoad();
                }
                catch (Exception ex)
                {
                    ExMessageBox.Show(ex.Message,AppSetter.SoftName);
                }
                finally
                {
                    // 设置鼠标默认状态
                    this.Cursor = Cursors.Default;
                }
            }
        }

       
    }
}